package it.nexsoft.dyn.compiler;

import java.util.HashMap;

public class DynClassRepos {

	HashMap<String, DynClass> classCache = new HashMap<String, DynClass>();
	
	
	private DynClassLoader current = null;
	public DynClassRepos() {
		current = new DynClassLoader(this, DynClassRepos.class.getClassLoader());
	}
	
	public DynClassLoader getCurrent() {
		return current;
	}
	
	public void add(DynClass dynClass) {
		
		classCache.put(dynClass.getClassName(), dynClass);
	}
	
	DynClass get(String clazzName) {		
		return classCache.get(clazzName);
	}
	
	public Class<?> getClass(String clazzName) {		
		DynClass ac = classCache.get(clazzName);
		if (ac == null)
			return null;
		
		return ac.getJavaClass();
	}
	
	public String getSource(String clazzName) {		
		DynClass ac = classCache.get(clazzName);
		if (ac == null)
			return null;
		
		return ac.getJavaSource();
	}
	
	public byte[] getByteCode(String clazzName) {		
		DynClass ac = classCache.get(clazzName);
		if (ac == null)
			return null;
		
		return ac.getJavaByteCode();
	}

	public void setCompiled(String className, byte[] classByteCode) {
		DynClass ac = classCache.get(className);
		if (ac == null)
			return;
		ac.setJavaByteCode(classByteCode);
		ac.setJavaClass(getCurrent().defineClassFromBytes(className, classByteCode));
	}

}
